from copy import copy

from kivy.app import App
from kivy.core.text import LabelBase, DEFAULT_FONT
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup

from kivy_cust.ConfigComm import ConfigComm

Window.set_icon('pixel_cat.png')  # 设置程序图标
LabelBase.register(DEFAULT_FONT, ConfigComm.FONT_SIMKAI)

# 创建登录页面类
# 这个版本自定义标签都是写在.kv文件里
class FormLayout(BoxLayout):
    next = 0
    tickets = []
    # id  valid  tel  pattern  begin_time  strategy  ground_ids  time_ids
    ticket_form = {}

    def refresh_table(self):
        # if self.tickets:
        #     if self.ids.ticket_blank in self.ids.ticket_content.children[0].children:
        #         self.remove_widget(self.ticket_blank)
        #     self.ticket_table.clear_widgets()
        #     for ticket in self.tickets:
        #         row_layout = Builder.load_string('<RowLayout>:')
        #         label_comm = Builder.load_string('<LabelComm>:'
        #                                          f'  text: "{ticket.id}"')
        #         row_layout.add_widget(label_comm)
        #         self.tickets.add_widget(row_layout)
        #     if self.ticket_table not in self.children:
        #         self.add_widget(self.ticket_table)
        # else:
        #     if self.ids.ticket_table in self.ids.ticket_content.children[0].children:
        #         self.remove_widget(self.ids.ticket_table)
        #     if self.ids.ticket_blank not in self.ids.ticket_content.children[0].children:
        #         self.ids.ticket_content.add_widget(self.ids.ticket_blank)
        pass

    def commit_form(self):
        print('commit_form')
        # 在此处编写保存信息的逻辑
        self.commit_before()

        ticket = copy(self.ticket_form)
        self.tickets.append(ticket)

        self.reset_form()

        self.refresh_table()

        print(f"ticket_form: {self.ticket_form}")


    def reset_form(self):
        print('reset_form')
        # 在此处编写重置信息的逻辑
        self.ticket_form = {}
        # id  valid  tel  pattern  begin_time  strategy  ground_ids  time_ids
        self.ids.tel.text = ''
        self.ids.grap.active = False
        self.ids.monitor.active = False
        self.ids.time_hour.text = ''
        self.ids.time_minute.text = ''
        self.ids.time_second.text = ''
        self.ids.strategy_00.active = False
        self.ids.strategy_01.active = False
        self.ids.strategy_02.active = False
        self.ids.ground_ids.text = '请选择门票场次'
        self.ids.time_ids.text = '请选择门票时间'


    def commit_before(self):

        self.ticket_form['id'] = self.next
        self.next = self.next + 1
        if self.ids.tel.text:
            self.ticket_form['tel'] = self.ids.tel.text
        if self.ids.time_hour.text and self.ids.time_minute.text and self.ids.time_second.text:
            self.ticket_form['begin_time'] = self.ids.time_hour.text + ':' + self.ids.time_minute.text + ':' + self.ids.time_second.text
        self.ticket_form.update(ground_ids=self.ids.ground_ids.text)
        self.ticket_form.update(time_ids=self.ids.time_ids.text)

        tips = ''
        if not self.ticket_form.get('tel'):
            tips += '手机号必输\n'
        if not self.ticket_form.get('pattern'):
            tips += '模式必输\n'
        if not self.ticket_form.get('begin_time'):
            tips += '执行时间必输\n'
        if not self.ticket_form.get('strategy'):
            tips += '策略必输\n'
        if not self.ticket_form.get('ground_ids') or '请选择' in self.ticket_form.get('ground_ids'):
            tips += '门票场次必输\n'
        if not self.ticket_form.get('time_ids') or '请选择' in self.ticket_form.get('time_ids'):
            tips += '门票时间必输\n'
        print(f'tips:{tips}')  # commit  tips
        if len(tips):
            tips = tips[:-1]
            popup = Popup(title='提示', content=Label(text=tips), size_hint=(None, None), size=(300, 200))
            popup.open()
            return False
        return True

    def pattern_check(self, checkbox):
        print(f'pattern_check:{checkbox.text} {checkbox.active}')
        if checkbox.active:
            self.ticket_form.update(pattern=checkbox.text)
        else:
            self.ticket_form.update(pattern=None)

    def strategy_check(self, checkbox):
        print(f'strategy_check:{checkbox.text} {checkbox.active}')
        if checkbox.active:
            self.ticket_form.update(strategy=checkbox.text)
        else:
            self.ticket_form.update(strategy=None)


class TicketApp(App):
    def build(self):
        self.title = '抢个票吧V1'
        return Builder.load_file('4-抢票页面V1.kv')

# 运行应用程序
if __name__ == '__main__':
    TicketApp().run()